﻿Imports System.Collections.ObjectModel
Imports System.ComponentModel

Public Class Customer
    Implements INotifyPropertyChanged

    Private vornam As String = ""
    Private nachnam As String = ""
    Private tite As String = ""
    Private kundennr As String = ""
    Private _anrede As String = ""
    Private _bezahlmethode As PaymentMethode
    Private _addresses As New ObservableCollection(Of EAddress)
    Private _iscompany As Boolean = False

    Private adresse As New Address
    Private xml As IDatabase
    Private _preisgruppe As Preisgruppe
    Private _kontakt As New Contact

    ''' <summary>
    ''' Erstellt eine Instanz zum Erstellen eines neuen Kunden
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New(Optional ByVal newcustomer As Boolean = False, Optional ByVal customers As Collection(Of CustomerPreviewModel) = Nothing)
        If newcustomer = True And customers IsNot Nothing Then
            CustomerID = GenerateKundennummer(customers)
        End If
    End Sub

    Public Property Preisgruppe As Preisgruppe
        Get
            Return _preisgruppe
        End Get
        Set(ByVal value As Preisgruppe)
            _preisgruppe = value
            NotifyPropertyChanged("Preisgruppe")
        End Set
    End Property

    Public Property Contact As Contact
        Set(ByVal value As Contact)
            _kontakt = value
            NotifyPropertyChanged("Contact")
        End Set
        Get
            Return _kontakt
        End Get
    End Property

    Public Property Address As Address
        Get
            Return adresse
        End Get
        Set(ByVal value As Address)
            adresse = value
            NotifyPropertyChanged("Address")
        End Set
    End Property

    Public Property CustomerID As String
        Get
            Return kundennr
        End Get
        Set(value As String)
            kundennr = value
            NotifyPropertyChanged("CustomerID")
        End Set
    End Property

    Public Property PaymentMethode As PaymentMethode
        Get
            Return _bezahlmethode
        End Get
        Set(value As PaymentMethode)
            _bezahlmethode = value
            NotifyPropertyChanged("PaymentMethode")
        End Set
    End Property

    Private Function GenerateKundennummer(ByVal kundenliste As Collection(Of CustomerPreviewModel)) As String
        Dim highnr As Integer = 0
        If kundenliste.Count = 0 Then
            Return 1000
        End If
        For Each c As CustomerPreviewModel In kundenliste
            Try
                If c.CustomerID > highnr Then
                    highnr = c.CustomerID
                End If
            Catch :End Try
        Next
        Return highnr + 1
    End Function

    Public Sub SetKundenNummer(ByVal value As String)
        kundennr = value
    End Sub

    Public Property Addresses As ObservableCollection(Of EAddress)
        Get
            Return _addresses
        End Get
        Set(value As ObservableCollection(Of EAddress))
            _addresses = value
            NotifyPropertyChanged("Addresses")
        End Set
    End Property

    Public Shared Function IsEmpty(customer As Customer) As Boolean
        Try
            If String.IsNullOrEmpty(customer.CustomerID) Then
                If (String.IsNullOrEmpty(customer.Address.Surname) = False OrElse String.IsNullOrEmpty(customer.Address.Forname) = False OrElse String.IsNullOrEmpty(customer.Address.Addition) = False) Then
                    Return False
                Else
                    Return True
                End If
            End If
            Return False
        Catch ex As Exception
            'null reference
            Return True
        End Try
    End Function

    Public ReadOnly Property DisplayName As String
        Get
            If IsCompany AndAlso Not String.IsNullOrEmpty(Address.CompanyName) Then
                Return Address.CompanyName
            End If
            If String.IsNullOrEmpty(Address.Surname) = False Then
                Return Address.Surname
            End If
            If String.IsNullOrEmpty(Address.Forname) = False Then
                Return Address.Forname
            End If
            Return Address.Addition
        End Get
    End Property

    Public Overrides Function Equals(obj As Object) As Boolean
        Try
            Dim temp As Customer = obj
            If temp.CustomerID = CustomerID Then
                Return True
            End If
            Return False
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function GenerateSampleCustomers() As List(Of Customer)
        Dim customer As New Customer With {.IsCompany = False, .Preisgruppe = 1, .CustomerID = 1000}
        With customer.Address
            .Forname = "Max"
            .Surname = "Mustermann"
            .Salutation = "Herr"
            .Title = "Dr."
            .Street = "Musterstraße"
            .No = 12
            .ZipCode = "12345"
            .City = "Musterstadt"
        End With

        Dim customer1 As New Customer With {.IsCompany = False, .Preisgruppe = 1, .CustomerID = 1001}
        With customer1.Address
            .Forname = "Karl-Heinz"
            .Surname = "Müller"
            .Salutation = "Herr"
            .Street = "Hafenweg"
            .No = 1
            .ZipCode = "23456"
            .City = "Hafencity"
        End With

        Dim customer2 As New Customer With {.IsCompany = False, .Preisgruppe = 1, .CustomerID = 1002}
        With customer2.Address
            .Forname = "Emma"
            .Surname = "Müller"
            .Salutation = "Frau"
            .Street = "Hafenweg"
            .No = 5
            .ZipCode = "23456"
            .City = "Hafencity"
        End With
        customer2.Addresses.Add(New EAddress() With {.AddressDescription = "Lieferadresse", .City = "Musterdorf"})
        Dim output As New List(Of Customer)
        output.Add(customer1)
        output.Add(customer2)
        output.Add(customer)
        Return output
    End Function

    '### Since Version 1.4 ###'
    ''' <summary>
    ''' Determines wheter the Customer is a company (for b2b) or a private person
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property IsCompany As Boolean
        Get
            Return _iscompany
        End Get
        Set(value As Boolean)
            _iscompany = value
            NotifyPropertyChanged("IsCompany")
        End Set
    End Property

    Protected Sub NotifyPropertyChanged(info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class